package com.test.reactor.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.test.reactor.model.mongo.City;
import com.test.reactor.repository.mongo.CityRepository;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@Component
public class CityHandler {

	@Autowired
    private CityRepository cityRepository;
    
    public CityHandler(CityRepository cityRepository) {
        this.cityRepository = cityRepository;
    }

    public Mono<City> save(City city) {
        return cityRepository.save(city);
    }

    public Mono<City> findCityById(Long id) {

        return cityRepository.findById(id);
    }

    public Flux<City> findAllCity() {
        return cityRepository.findAll().flatMap(city->{
        	city.setCityName(city.getCityName() + "----tttt");
        	return Mono.just(city);
        });
    }

    public Mono<City> modifyCity(City city) {

        return cityRepository.save(city);
    }
    
    public Mono<Long> findCitySize() {
    	return cityRepository.count();
    }

    public Mono<Long> deleteCity(Long id) {
        cityRepository.deleteById(id);
        return Mono.create(cityMonoSink -> cityMonoSink.success(id));
    }
}